#!coding=utf-8
from django.shortcuts import *
from models import *
import simplejson
from django.views.decorators.csrf import csrf_exempt
from util.form_util  import *
from django.http import HttpResponseRedirect
from django.core.paginator import Paginator, PageNotAnInteger, EmptyPage
from enterprise_pollute.models import *

def preventionDetail(request):
    _enterPrise = request.session.get('ep')
    fillModel=request.session['fill']
    appModel=Application_status.objects.filter(fill=fillModel,enterprise=_enterPrise,app_name="污染物基本信息")
    if appModel:
        appModel=appModel[0]
    water=Water.objects.filter(enterprise=_enterPrise,application_status=appModel)
    exhaust=Exhaust.objects.filter(enterprise=_enterPrise,application_status=appModel)
    solid=Solid.objects.filter(enterprise=_enterPrise,application_status=appModel)
    status=None
    if water:
        water=water[0]
        status=water.submit_type
    if exhaust:
        exhaust=exhaust[0]
    if solid:
        solid=solid[0]
    params={'water':water,
            'exhaust':exhaust,
            'solid':solid,
            'status':status,
            'fillModel':fillModel.status}
    return  render_to_response('enterprise/prevention_info_detail.html',params,context_instance=RequestContext(request))
#编辑污染防治基本信息
@csrf_exempt
def preventionEdit(request):
     _enterPrise = request.session.get('ep')
     fillModel=request.session['fill']
     appModel=Application_status.objects.filter(fill=fillModel,enterprise=_enterPrise,app_name="污染物基本信息")
     if appModel:
        appModel=appModel[0]
     if request.method == "GET":
         try:
            #废气
            exhaust = Exhaust.objects.filter(enterprise = _enterPrise,application_status=appModel)
            #废水
            water=Water.objects.filter(enterprise=_enterPrise,application_status=appModel)
            #固废
            solid=Solid.objects.filter(enterprise=_enterPrise,application_status=appModel)
            if len(exhaust) > 0:
                exhaust = exhaust[0]
            if len(water) > 0:
                water = water[0]
            if len(solid) > 0:
                solid = solid[0]
            params={  'exhaust' : exhaust ,
                      'water':water,
                      'solid':solid}
            return render_to_response('enterprise/prevention_info.html', params ,context_instance=RequestContext(request))
         except Exception as e:
            return HttpResponse("非法请求\n\n"+e.message)
     if request.method=='POST':
         water_produce=request.POST.get('water_produce')
         water_let=request.POST.get('water_let')
         is_takeover=request.POST.get('is_takeover')
         water_let_type=request.POST.get('water_let_type')
         last_water_name=request.POST.get('last_water_name')
         w_type=request.POST.getlist('w_type')
         w_count=request.POST.getlist('w_count')
         exhaust_let=request.POST.get('exhaust_let')
         e_count=request.POST.get('e_count')
         e_t_type=request.POST.getlist('e_t_type')
         e_t_count=request.POST.getlist('e_t_count')
         common_number=request.POST.get('common_number')
         wx_number=request.POST.get('wx_number')
         wxfw_name=request.POST.get('wxfw_name')
         wxfw_type=request.POST.getlist('wxfw_type')
         exhaust=Exhaust.objects.filter(enterprise = _enterPrise,application_status=appModel)
         water=Water.objects.filter(enterprise=_enterPrise,application_status=appModel)
         Solid.objects.filter(enterprise=_enterPrise,application_status=appModel).delete()
         if exhaust:
            exhaust=exhaust[0]
            ExhaustTotalemission.objects.filter(exhaust=exhaust).delete()
            Exhaust.objects.filter(enterprise = _enterPrise,application_status=appModel).delete()
         if water:
            water=water[0]
            WaterTotalemission.objects.filter(water=water).delete()
            Water.objects.filter(enterprise=_enterPrise,application_status=appModel).delete()
         try:
            key= request.GET['key']
            exhaustModel=Exhaust()
            exhaustModel.enterprise=_enterPrise
            exhaustModel.exhaust_let=exhaust_let
            exhaustModel.count=e_count
            exhaustModel.application_status=appModel
            exhaustModel.submit_type='2'
            exhaustModel.save()
            one_number=len(e_t_type)
            ETotalemission(one_number,e_t_type,e_t_count,exhaustModel)
            waterModel=Water()
            waterModel.enterprise=_enterPrise
            waterModel.is_takeover=is_takeover
            waterModel.last_water_name=last_water_name
            waterModel.water_let=water_let
            waterModel.water_produce=water_produce
            waterModel.water_let_type=water_let_type
            waterModel.application_status=appModel
            waterModel.submit_type='2'
            waterModel.save()
            WTotalemission(len(w_type),w_type,w_count,waterModel)
            sd=Solid()
            sd.enterprise=_enterPrise
            sd.common_number=common_number
            sd.application_status=appModel
            sd.submit_type='2'
            appModel.app_status="未确认"
            appModel.save()
            sd.wx_number=wx_number
            sd.wxfw_name=wxfw_name
            wt=''
            len(wxfw_type)
            for number in range(len(wxfw_type)):
                wt=wt+wxfw_type[number]
            sd.wxfw_type=wt
            sd.save()
            return HttpResponseRedirect("/pollution/prevention/detail/")
         except Exception as e:
             exhaustModel=Exhaust()
             exhaustModel.enterprise=_enterPrise
             exhaustModel.exhaust_let=exhaust_let
             exhaustModel.count=e_count
             exhaustModel.application_status=appModel
             exhaustModel.submit_type='1'
             exhaustModel.save()
             one_number=len(e_t_type)
             ETotalemission(one_number,e_t_type,e_t_count,exhaustModel)
             waterModel=Water()
             waterModel.enterprise=_enterPrise
             waterModel.is_takeover=is_takeover
             waterModel.last_water_name=last_water_name
             waterModel.water_let=water_let
             waterModel.water_produce=water_produce
             waterModel.water_let_type=water_let_type
             waterModel.application_status=appModel
             waterModel.submit_type='1'
             waterModel.save()
             WTotalemission(len(w_type),w_type,w_count,waterModel)

             sd=Solid()
             sd.enterprise=_enterPrise
             sd.common_number=common_number
             sd.wx_number=wx_number
             sd.application_status=appModel
             sd.submit_type='1'
             sd.wxfw_name=wxfw_name
             wt=''
             len(wxfw_type)
             for number in range(len(wxfw_type)):
                wt=wt+wxfw_type[number]
             sd.wxfw_type=wt
             sd.save()
             appModel.app_status="已确认"
             appModel.save()
             return HttpResponseRedirect("/pollution/prevention/detail/")

def WTotalemission(one_number,w_type,w_count,waterModel):
    for number in range(one_number):
        wt=WaterTotalemission()
        wt.type=w_type[number]
        wt.count=w_count[number]
        wt.water=waterModel
        wt.save()

def ETotalemission(one_number,e_t_type,e_t_count,exhaustModel):
    for number in range(one_number):
        et=ExhaustTotalemission()
        et.count=e_t_count[number]
        et.type=e_t_type[number]
        et.exhaust=exhaustModel
        et.save()

@csrf_exempt
def transferList(request):
    _enterPrise = request.session.get('ep')
    fillModel=request.session['fill']
    appModel=Application_status.objects.filter(fill=fillModel,enterprise=_enterPrise,app_name="特征化学污染物信息")
    if appModel:
        appModel=appModel[0]
    status=appModel.app_status
    basic_list = FeaturesPollu.objects.filter(enterprise = _enterPrise,application_status=appModel)
    try:
        paginator = Paginator(basic_list, 10)
        page = request.GET.get('page')
        basic_list = paginator.page(page)
    except PageNotAnInteger:
        basic_list = paginator.page(1)
    except EmptyPage:
        basic_list = paginator.page(paginator.num_pages)
    return  render_to_response('enterprise/prevention_releaseTransferList.html',{'fillModel':fillModel.status,"status":status,"list":basic_list, 'paginator':paginator },context_instance=RequestContext(request))

@csrf_exempt
def transferAdd(request):
    _enterPrise=request.session.get('ep')
    if request.method=="GET":
        product=Product.objects.filter(enterprise=_enterPrise)
        params={'product':product}
        return  render_to_response('enterprise/prevention_releaseTransfer.html',params,context_instance=RequestContext(request))
    if request.method=="POST":
        fillModel=request.session['fill']
        appModel=Application_status.objects.filter(fill=fillModel,enterprise=_enterPrise,app_name="特征化学污染物信息")
        if appModel:
            appModel=appModel[0]
        name=request.POST.get("name")
        type=request.POST.get("type")
        cas=request.POST.get("cas")
        product=request.POST.get("product")
        rel_condition=request.POST.get("rel_condition")
        rel_yes=request.POST.get("rel_yes")
        rel_no=request.POST.get("rel_no")
        rel_water=request.POST.get("rel_water")
        rel_tu=request.POST.get("rel_tu")
        rel_total=request.POST.get("rel_total")
        transform_condition=request.POST.get("transform_condition")
        transform_water=request.POST.get("transform_water")
        transform_gf=request.POST.get("transform_gf")
        transform_total=request.POST.get("transform_total")
        adjust=request.POST.get("adjust")
        report_by=request.POST.get("report_by")
        report_file=request.FILES.get("report_file",'')
        featuresPoll=FeaturesPollu()
        featuresPoll.name=name
        featuresPoll.type=type
        featuresPoll.rel_condition=rel_condition
        featuresPoll.rel_yes=rel_yes
        featuresPoll.rel_no=rel_no
        featuresPoll.rel_water=rel_water
        featuresPoll.rel_tu=rel_tu
        featuresPoll.rel_total=rel_total
        featuresPoll.transform_condition=transform_condition
        featuresPoll.transform_water=transform_water
        featuresPoll.transform_gf=transform_gf
        featuresPoll.transform_total=transform_total
        featuresPoll.adjust=adjust
        featuresPoll.report_by=report_by
        featuresPoll.report_file=report_file
        featuresPoll.enterprise=_enterPrise
        featuresPoll.application_status=appModel
        featuresPoll.cas=cas
        featuresPoll.submit_type='2'
        appModel.app_status="未确认"
        appModel.save()
    product=Product.objects.get(id=product)
    featuresPoll.product=product
    featuresPoll.save()
    return  HttpResponseRedirect("/pollution/release_transfer/transferList/")
@csrf_exempt
def transferEdit(request):
    _enterPrise = request.session.get('ep')
    fillModel=request.session['fill']
    appModel=Application_status.objects.filter(fill=fillModel,enterprise=_enterPrise,app_name="特征化学污染物信息")
    if appModel:
        appModel=appModel[0]
    if request.method == "GET":
         try:
             id = request.GET["id"]
             basic_list = FeaturesPollu.objects.filter(id = id)
             if len(basic_list) > 0:
                 basic = basic_list[0]
             product=Product.objects.filter(enterprise=_enterPrise)
             params={'product':product,'basic':basic}
             return render_to_response('enterprise/prevention_releaseTransfer.html',params,context_instance=RequestContext(request))
         except Exception as e:
             return HttpResponse("非法请求\n\n"+e.message)
    if request.method=="POST":
        fillModel=request.session['fill']
        appModel=Application_status.objects.filter(fill=fillModel,enterprise=_enterPrise,app_name="特征化学污染物信息")
        if appModel:
            appModel=appModel[0]
        name=request.POST.get("name")
        type=request.POST.get("type")
        cas=request.POST.get("cas")
        product=request.POST.get("product")
        rel_condition=request.POST.get("rel_condition")
        rel_yes=request.POST.get("rel_yes")
        rel_no=request.POST.get("rel_no")
        rel_water=request.POST.get("rel_water")
        rel_tu=request.POST.get("rel_tu")
        rel_total=request.POST.get("rel_total")
        transform_condition=request.POST.get("transform_condition")
        transform_water=request.POST.get("transform_water")
        transform_gf=request.POST.get("transform_gf")
        transform_total=request.POST.get("transform_total")
        adjust=request.POST.get("adjust")
        report_by=request.POST.get("report_by")
        report_file=request.FILES.get("report_file",'')
        id=request.POST.get("id")
        if id:
            featuresPoll=FeaturesPollu.objects.get(id=id)
            featuresPoll.name=name
            featuresPoll.type=type
            featuresPoll.rel_condition=rel_condition
            featuresPoll.rel_yes=rel_yes
            featuresPoll.rel_no=rel_no
            featuresPoll.rel_water=rel_water
            featuresPoll.rel_tu=rel_tu
            featuresPoll.rel_total=rel_total
            featuresPoll.transform_condition=transform_condition
            featuresPoll.transform_water=transform_water
            featuresPoll.transform_gf=transform_gf
            featuresPoll.transform_total=transform_total
            featuresPoll.adjust=adjust
            featuresPoll.report_by=report_by
            featuresPoll.report_file=report_file
            featuresPoll.enterprise=_enterPrise
            featuresPoll.application_status=appModel
            featuresPoll.cas=cas
            featuresPoll.submit_type='2'
            appModel.app_status="未确认"
            appModel.save()
        product=Product.objects.get(id=product)
        featuresPoll.product=product
        featuresPoll.save()
        return  HttpResponseRedirect("/pollution/release_transfer/transferList/")
def transferDel(request):
    id = request.GET["id"]
    _enterPrise = request.session.get('ep')
    fillModel=request.session['fill']
    appModel=Application_status.objects.filter(fill=fillModel,enterprise=_enterPrise,app_name="特征化学污染物信息")
    if appModel:
        appModel=appModel[0]
    FeaturesPollu.objects.filter(id = id).delete()
    featuresList=FeaturesPollu.objects.filter(enterprise=_enterPrise,application_status=appModel)
    count=0
    scount=0
    for feat in featuresList:
        if feat.submit_type=='2':
            count=count+1
        if feat.submit_type=='1':
            scount=scount+1
    if count==0 and scount!=0:
        appModel.app_status="已确认"
    else:
        appModel.app_status="未确认"
    appModel.save()
    return HttpResponseRedirect("/pollution/release_transfer/transferList/")

def transferDetail(request):
    id = request.GET["id"]
    basic = FeaturesPollu.objects.get(id = id)
    return  render_to_response('enterprise/prevention_releaseTransfer_detail.html',{"basic":basic},context_instance=RequestContext(request))

@csrf_exempt
def submitTransfer(request):
    _enterPrise = request.session.get('ep')
    fillModel=request.session['fill']
    appModel=Application_status.objects.filter(fill=fillModel,enterprise=_enterPrise,app_name="特征化学污染物信息")
    if appModel:
        appModel=appModel[0]
    name=request.POST.get("name")
    type=request.POST.get("type")
    cas=request.POST.get("cas")
    product=request.POST.get("product")
    rel_condition=request.POST.get("rel_condition")
    rel_yes=request.POST.get("rel_yes")
    rel_no=request.POST.get("rel_no")
    rel_water=request.POST.get("rel_water")
    rel_tu=request.POST.get("rel_tu")
    rel_total=request.POST.get("rel_total")
    transform_condition=request.POST.get("transform_condition")
    transform_water=request.POST.get("transform_water")
    transform_gf=request.POST.get("transform_gf")
    transform_total=request.POST.get("transform_total")
    adjust=request.POST.get("adjust")
    report_by=request.POST.get("report_by")
    report_file=request.FILES.get("report_file",'')
    id=request.POST.get("id")
    if id:
        featuresPoll=FeaturesPollu.objects.get(id=id)
    else:
        featuresPoll=FeaturesPollu()
    featuresPoll.name=name
    featuresPoll.type=type
    featuresPoll.rel_condition=rel_condition
    featuresPoll.rel_yes=rel_yes
    featuresPoll.rel_no=rel_no
    featuresPoll.rel_water=rel_water
    featuresPoll.rel_tu=rel_tu
    featuresPoll.rel_total=rel_total
    featuresPoll.transform_condition=transform_condition
    featuresPoll.transform_water=transform_water
    featuresPoll.transform_gf=transform_gf
    featuresPoll.transform_total=transform_total
    featuresPoll.adjust=adjust
    featuresPoll.report_by=report_by
    featuresPoll.report_file=report_file
    featuresPoll.enterprise=_enterPrise
    featuresPoll.application_status=appModel
    featuresPoll.cas=cas
    featuresPoll.submit_type='1'
    product=Product.objects.get(id=product)
    featuresPoll.product=product
    featuresPoll.save()
    featuresList=FeaturesPollu.objects.filter(enterprise=_enterPrise,application_status=appModel)
    count=0
    scount=0
    for feat in featuresList:
        if feat.submit_type=='2':
            count=count+1
        if feat.submit_type=='1':
            scount=scount+1
    print 'count:',count,'scount:',scount
    if count==0 and scount>0:
        appModel.app_status="已确认"
        appModel.save()
    return  HttpResponseRedirect("/pollution/release_transfer/transferList/")

def submitAllMenu(request):
    _enterPrise = request.session.get('ep')
    fillModel=request.session['fill']
    appModel=Application_status.objects.filter(fill=fillModel,enterprise=_enterPrise,app_name="特征化学污染物信息")
    if appModel:
        appModel=appModel[0]
    appModel.app_status="已提交"
    featuresPoll=FeaturesPollu.objects.filter(enterprise = _enterPrise,application_status=appModel)
    for features in featuresPoll:
        features.submit_type='1'
        features.save()
    appModel.save()
    return  HttpResponseRedirect("/pollution/release_transfer/transferList/")

@csrf_exempt
def chemicalsList(request):
    _enterPrise = request.session.get('ep')
    if request.method == "POST":
        if request.POST["create_time"]:
            create_time = request.POST["create_time"]
            create_time = create_time.split("-")
            year = create_time[0]
            month = create_time[1]
            basic_list = other_specific_information.objects.filter(enterprise = _enterPrise,create_time__year = year,create_time__month = month)
        else:
            basic_list = other_specific_information.objects.filter(enterprise = _enterPrise)
    else:
        basic_list = other_specific_information.objects.filter(enterprise = _enterPrise)
    try:
        paginator = Paginator(basic_list, 10)
        page = request.GET.get('page')
        basic_list = paginator.page(page)
    except PageNotAnInteger:
        basic_list = paginator.page(1)
    except EmptyPage:
        basic_list = paginator.page(paginator.num_pages)
    return  render_to_response('enterprise/prevention_otherChemicalsList.html',{"list":basic_list, 'paginator':paginator},context_instance=RequestContext(request))

def chemicalAdd(request):
    return  render_to_response('enterprise/prevention_otherChemical.html',context_instance=RequestContext(request))
@csrf_exempt
def chemicalEdit(request):
    _enterPrise = request.session.get('ep')
    if request.method == "GET":
        try:
            id = request.GET["id"]
            basic_list = other_specific_information.objects.filter(id = id)
            if len(basic_list) > 0:
                basic = basic_list[0]
                return render_to_response('enterprise/prevention_otherChemical.html',{"basic":basic},context_instance=RequestContext(request))
            else:
                return render_to_response('enterprise/prevention_otherChemical.html',context_instance=RequestContext(request))
        except Exception as e:
            return HttpResponse("非法请求\n\n"+e.message)
    if request.method == "POST":
        try:
            if "id" in request.POST and request.POST['id']:
                basic_list = other_specific_information.objects.filter(id = request.POST['id'])
                basic = basic_list[0]
            else:
                basic = other_specific_information()
                 #装填菜单
            auto_wear(request,basic,"enterprise_pollution_prevention",'other_specific_information')
               
            basic.enterprise = _enterPrise
            basic.save()
            return HttpResponseRedirect("/pollution/other/chemicalsList/")
        except Exception as e:
           result = {'error':e.message if '' else (e.__str__()) }
           return HttpResponse(simplejson.dumps(result))

def chemicalDel(request):
    id = request.GET["id"]
    other_specific_information.objects.filter(id = id).delete()
    return HttpResponseRedirect("/pollution/other/chemicalsList/")

def chemicalDetail(request):
    id = request.GET["id"]
    basic = other_specific_information.objects.get(id = id)
    return  render_to_response('enterprise/prevention_otherChemical_detail.html',{"basic":basic},context_instance=RequestContext(request))
